﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 配置明细表 数据类
    /// </summary>
    public class ConfigEntry
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigEntry()
        {
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="keyName">关键字</param>
        /// <param name="configClass">配置类别</param>
        /// <returns></returns>
        public DataSet GetList(string entityNo, string keyName, string configClass)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FDocClsCd,FEntityNo,FKeyName,FConfigClass ");
            strSql.Append(" FROM t_ConfigEntry ");
            strSql.Append(" where FEntityNo=@FEntityNo and FKeyName=@FKeyName and FConfigClass=@FConfigClass ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FKeyName", SqlDbType.NVarChar,50),
					new SqlParameter("@FConfigClass", SqlDbType.Char,2)			};
            parameters[0].Value = entityNo;
            parameters[1].Value = keyName;
            parameters[2].Value = configClass;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2013-09-03</date>
        /// </summary>
        /// <param name="row">对象</param>
        /// <returns>实体类</returns>
        public ElingOA6_Admin_DSJCJ.Model.ConfigEntry DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ConfigEntry model = new ElingOA6_Admin_DSJCJ.Model.ConfigEntry();
            if (row != null)
            {
                if (row["FDocClsCd"] != null)
                {
                    model.FDocClsCd = row["FDocClsCd"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FKeyName"] != null)
                {
                    model.FKeyName = row["FKeyName"].ToString();
                }
                if (row["FConfigClass"] != null)
                {
                    model.FConfigClass = row["FConfigClass"].ToString();
                }
            }
            return model;
        }


        /// <summary>
        /// 获得数据列表
        /// <creator>sunlj</creator>
        /// <date>2014-5-9</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="configClass">配置类别</param>
        /// <returns></returns>
        public DataSet GetListWithShowText(string entityNo, string configClass)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"WITH cte AS 
(SELECT dbo.t_docCls.FEntityNo,dbo.t_docCls.FDocClsCd,FPreDocClsCd,FDocClsName AS FShowText,FConfigClass,FKeyName
 FROM dbo.t_docCls,
dbo.t_ConfigEntry WHERE dbo.t_docCls.FDocClsCd=dbo.t_ConfigEntry.FDocClsCd
AND FConfigClass=@FConfigClass AND dbo.t_ConfigEntry.FEntityNo=@FEntityNo
UNION ALL
SELECT a.FEntityNo,a.FDocClsCd,a.FPreDocClsCd,a.FDocClsName AS FShowText,FConfigClass,FKeyName
 FROM dbo.t_docCls a,cte b
WHERE a.FPreDocClsCd=b.FDocClsCd
AND FConfigClass=@FConfigClass AND a.FEntityNo=@FEntityNo 
)
SELECT * FROM cte");

            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FConfigClass", SqlDbType.Char,2)			};
            parameters[0].Value = entityNo;
            parameters[1].Value = configClass;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

    }
}
